package com.juyu.miliao.authentication.dao.mysql;

import com.juyu.miliao.authentication.domain.UserLogin;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Repository
public class UserLoginDao {
    @Autowired
    private SqlSession sqlSession;

    public Optional<UserLogin> getUserByName(String username) {
        UserLogin userLogin = (UserLogin) sqlSession.selectOne("getUserByName", username);
        return Optional.ofNullable(userLogin);
    }
    public UserLogin getUserLoginByName(String username) {
        UserLogin userLogin = (UserLogin) sqlSession.selectOne("getUserByUserName", username);
        return userLogin;
    }

    public Optional<UserLogin> getUserByOpenId(String openId) {
        UserLogin userLogin = (UserLogin) sqlSession.selectOne("getUserByOpenId", openId);
        return Optional.ofNullable(userLogin);
    }

    public int updateUserLogin(UserLogin userLogin) {
        return sqlSession.update("updateUserLogin", userLogin);
    }

    public int addUserLogin(UserLogin userLogin) {
        return sqlSession.update("addUserLogin", userLogin);
    }

    public Optional<UserLogin> getUserByDeviceNo(String deviceNo) {
        UserLogin userLogin = (UserLogin) sqlSession.selectOne("getUserByDeviceNo", deviceNo);
        return Optional.ofNullable(userLogin);
    }


    public void updateUserLoginForDeviceNo(String deviceNo, Long userId) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("deviceNo",deviceNo);
        map.put("userId",userId);
        sqlSession.update("updateUserLoginForDeviceNo",map);
    }

}
